#!/usr/bin/python
# encoding:utf8
# cosine 2010-05

"""test
"""

import re
import os
import urllib
from kscomm.spider.start import Spider

g_down_dir = 'mm/'

def img_filter(url):
	p = re.compile('http://[^/]*/UploadFiles/\d{,6}/\d+\.jpg',re.I)
	if p.match(url):
		return True
	else:
		return False

def down_mm(url):
	mm = url.split('/')[-1]
	urllib.urlretrieve(url,g_down_dir+mm)

def func(argv_dict):
	url = argv_dict['url']
	deep = argv_dict['deep']
	if img_filter(url):
		print url
		down_mm(url)

	
if __name__ == '__main__':
	if not os.path.exists(g_down_dir):
		os.mkdir(g_down_dir)

	s = Spider('http://iphone.tgbus.com/ziyuan/pic/Index.shtml',deep=2,thread_num=5,crawl_scope=1,url_scope=1,url_type=1,crawl_tags=['a'],url_tags=['img'])
	s.set_callback(func,['url','deep'])
	s.start()
	s.get_time_statistic()
